﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pahan.McuFusion.IO
{
    public class ParallelPort : IDisposable
    {
        public ParallelPort()
        {
            impl = ParallelPortImplFactory.Create();
        }
        
        #region Implementation of IParallelPortImpl

        /// <summary>
        /// Opens the port.
        /// </summary>
        public void Open()
        {
            impl.Open();
        }

        /// <summary>
        /// Closes the port.
        /// </summary>
        public void Close()
        {
            impl.Close();
        }

        /// <summary>
        /// Gets the value indicating whether the port is opened.
        /// </summary>
        public bool IsOpened
        {
            get { return impl.IsOpened; }
        }

        ///// <summary>
        ///// Gets or sets the base address.
        ///// </summary>
        //public ushort BaseAddress
        //{
        //    get { throw new System.NotImplementedException(); }
        //    set { throw new System.NotImplementedException(); }
        //}

        /// <summary>
        /// Gets or sets the value of data i/o port.
        /// </summary>
        public byte DataPort
        {
            get { return impl.DataPort; }
            set { impl.DataPort = value; }
        }

        /// <summary>
        /// Gets or sets the value of control i/o port.
        /// </summary>
        public byte ControlPort
        {
            get { return impl.ControlPort; }
            set { impl.ControlPort = value; }
        }

        /// <summary>
        /// Gets the value of status i/o port.
        /// </summary>
        public byte StatusPort
        {
            get { return impl.StatusPort; }
        }

        /// <summary>
        /// Reads the value from the specified pin.
        /// </summary>
        public bool ReadPin(ParallelPortPins pin)
        {
            return impl.ReadPin(pin);
        }

        /// <summary>
        /// Writes the value to the specified pin.
        /// </summary>
        public void WritePin(ParallelPortPins pin, bool value)
        {
            impl.WritePin(pin, value);
        }

        #endregion

        #region Implementation of IDisposable

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            impl.Close();
        }

        #endregion

        #region Private Members

        private IParallelPortImpl impl;

        #endregion
    }
}
